package com.book.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

@WebServlet("/borrow")
public class borrow extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Connection conn = null;
        PreparedStatement ps = null;
        String id = req.getParameter("id");
        HttpSession session = req.getSession();
        String iduser = (String) session.getAttribute("iduser");
        int  borrow_num=0;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/library?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true", "root", "1234");
            String sql = String.format("select * from book where id = " + id);
            ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                String borrow = rs.getString(7);
                if (borrow != null) {
                    req.getRequestDispatcher("/worryborrow.jsp").forward(req, resp);
                } else {
                    String borrow_sql = String.format("select * from book where borrow = " + iduser);
                    ps = conn.prepareStatement(borrow_sql);
                    rs = ps.executeQuery();
                    while (rs.next()) {
                        borrow_num=borrow_num+1;
                    }
                    if(borrow_num>=5){
                        req.getRequestDispatcher("/borrow_too.jsp").forward(req, resp);
                    }else{
                        String Updatesql = String.format("Update book SET borrow='%s' where id='%s'", iduser, id);
                        ps = conn.prepareStatement(Updatesql);
                        ps.executeUpdate();
                        req.getRequestDispatcher("/successborrow.jsp").forward(req, resp);
                    }
                }
            }
            rs.close();
            conn.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}